package com.wuyou.mid;

import java.text.NumberFormat;

/**
 * 面试题 05.02. 二进制数转字符串
 */
public class PrintBin {


    public static void main(String[] args) {
        double num = 0.625;


        System.out.println(sol_1(num));
    }


    public static String sol_1(double num) {
        StringBuilder sb = new StringBuilder("0.");

        int i = 0;
        for (; i < 30; i++) {

            double temp = num * 2;
            int dec = (int) temp;
            if (dec != 0 || temp - dec != 0) {

                if (temp >= 1) {
                    sb.append(dec);
                } else {
                    sb.append(0);
                }
                num = temp - dec;
            } else {
                break;
            }

        }
        return i == 30 ? "error" : sb.toString();
    }
}
